/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import java.util.List;
import java.util.Vector;

import org.openeccos.EccosDesktop;
import org.openeccos.internal.PDFooterButton;
import org.openeccos.model.MTask;
import org.openeccos.properties.WzdAbstractProperties;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.SplitPane;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.layout.SplitPaneLayoutData;
import nextapp.echo2.contrib.app.effect.PulsateEffectCommand;
import echopointng.LabelEx;

/**
 * A wizard-like window, where the user can accomplish tasks step-by-step
 * in a guide way
 *
 * @author cgspinner@web.de
 */
public class PDWizard extends PDWindowPane {

	protected Button btnBack;
	protected Button btnNext;
	protected List<PDWizardPanel> panels = new Vector<PDWizardPanel>();
	protected int pageIndex = 0;
	protected LabelEx lblError;
	protected Column container;
	protected MTask originTask;

	public PDWizard() {
		this(null);
	}

	public PDWizard(MTask task) {
		this.originTask = task;
		initGUI();
	}

	protected void addPanel(PDWizardPanel pnl) {
		panels.add(pnl);
	}

	protected void initGUI() {
		setWidth(new Extent(650));
		setHeight(new Extent(400));
		setResizable(false);

		SplitPane split = new SplitPane(SplitPane.ORIENTATION_VERTICAL_BOTTOM_TOP);
		split.setSeparatorPosition(new Extent(24));
		add(split);

		SplitPane footerSplit = new SplitPane(SplitPane.ORIENTATION_HORIZONTAL_RIGHT_LEFT);
		footerSplit.setSeparatorPosition(new Extent(170));
		footerSplit.setBackground(colorScheme.getBackgroundDark());
		split.add(footerSplit);

		SplitPaneLayoutData spld = new SplitPaneLayoutData();
		spld.setOverflow(SplitPaneLayoutData.OVERFLOW_HIDDEN);

		Row footerRow = new Row();
		footerRow.setAlignment(new Alignment(Alignment.RIGHT, Alignment.DEFAULT));
		footerRow.setCellSpacing(new Extent(6));
		footerRow.setBackground(colorScheme.getBackgroundDark());
		footerRow.setInsets(new Insets(0, 3, 12, 5));
		footerRow.setLayoutData(spld);
		footerSplit.add(footerRow);

		btnBack = new PDFooterButton(WzdAbstractProperties.back());
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnBackClicked();
			}
		});
		footerRow.add(btnBack);

		btnNext = new PDFooterButton(WzdAbstractProperties.next());
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnNextClicked();
			}
		});
		footerRow.add(btnNext);

		lblError = new LabelEx();
		lblError.setForeground(Color.WHITE);
		lblError.setInsets(new Insets(6, 3, 0, 0));
		lblError.setLayoutData(spld);
		footerSplit.add(lblError);

		container = new Column();
		container.setInsets(new Insets(3, 8, 3, 3));
		split.add(container);
	}

	protected void btnNextClicked() {

		PDWizardPanel page = panels.get(pageIndex);
		//deprecated
		if (!page.doNextAction()) {
			return;
		}

		String error = page.getPreApplyError();
		if (error != null) {
			showError(error);
			return;
		}
		
		page.applyToModel();
		
		error = page.getError();
		if (error != null) {
			showError(error);
			return;
		}

		pageIndex++;
		if (pageIndex >= panels.size()) {
			setVisible(false);
			EccosDesktop.getCurrentPerspective().updateMenu();
		} else {
			showPage();
		}
	}

	protected void btnBackClicked() {
		if (panels.get(pageIndex).doBackAction()) {
			pageIndex--;
			showPage(false);
		}
	}

	protected void showPage() {
		showPage(true);
	}

	protected void showPage(boolean readFromModel) {
		container.removeAll();
		PDWizardPanel page = panels.get(pageIndex);
		if (readFromModel) {
			page.readFromModel();
		}
		container.add(page);

		if (page.getNextCaption() == null) {
			btnNext.setVisible(false);
		} else {
			btnNext.setVisible(true);
			btnNext.setText(page.getNextCaption());
		}
		if (page.getBackCaption() == null) {
			btnBack.setVisible(false);
		} else {
			btnBack.setVisible(true);
			btnBack.setText(page.getBackCaption());
		}
		lblError.setText("");

	}

	public void showError(String error) {
		lblError.setText(error);
		PulsateEffectCommand cmd = new PulsateEffectCommand();
		cmd.setComponent(lblError);
		ApplicationInstance.getActive().enqueueCommand(cmd);
	}
}
